﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
//using Shop.Models;
//using Shop.Services.Articles;
//using Shop.Services.Categories;
//using Shop.Infrastructure.Basket;
using AutoMapper;
//using Shop.Services.Orders;
using System.Text;
using Dinamico.Models;
using Dinamico.Service;

namespace Dinamico.Controllers
{
    public class BasketController : Controller
    {

        private IBasketService basketService;
        private IMailService mailService; 
        
        public BasketController(
            IBasketService basketService,
            IMailService mailService
            )
        {
            this.basketService = basketService;
            this.mailService = mailService;
        }

        // POST: JSON
        [HttpPost]
        public ActionResult UpdateBasket(BasketItemInputModel articleInputModel)
        {
            if (ModelState.IsValid && articleInputModel != null && articleInputModel.ArticleId > 0)
            {
                try
                {
                    //Update top basket
                    basketService.Update(articleInputModel);
                }
                catch (ArgumentException ex)
                {
                    return Content(ex.ToString());
                }

                return Json(articleInputModel);
            }

            return Content("An error occured!");
        }

        // POST: JSON
        [HttpPost]
        public ActionResult RemoveFromBasket(string basketId)
        {
            try
            {
                basketService.RemoveById(basketId);
            }
            catch (ArgumentException ex)
            {
                return Content(ex.ToString());
            }
            return Json(new BasketItemModel());
        }


        [HttpPost]
        public ActionResult RemoveAllFromBasket()
        {
            try
            {
                basketService.RemoveAll();
            }
            catch (ArgumentException ex)
            {
                return Content(ex.ToString());
            }
            return Json(new BasketItemModel());
        }

        [ChildActionOnly]
        public ActionResult GetUserBasketContent()
        {

            return View("UserBasketControl", basketService.BasketListToBasketItemModels());
        }

        [HttpPost]
        public ActionResult GetUserBasketJsonContent()
        {
         
            return Json(basketService.BasketListToBasketItemModels());
        }


        [HttpPost]
        public ActionResult ConfirmOrder(JsonAddressModel model)
        {
            //send mail
            var basketItems = basketService.BasketListToBasketItemModels();
            mailService.SendOrderMail(model, basketItems);
            //todo save in DB
            return Json(basketItems);
        }


        /// <summary>
        /// Get the List ORDERED BY NAMES DESCENDING
        /// </summary>
        /// <returns></returns>
 
        public ActionResult BasketList()
        {
            return View(basketService.BasketListToBasketItemModels());
        }

        [ChildActionOnly]
        public ActionResult VatAndDeliveryText()
        {
            var sb = new StringBuilder(String.Empty);

            //sb.AppendFormat(Resources.Basket.UserBasketControl.VatText, String.Format("{0:F}", orderHelperService.VatRate));
            //sb.AppendLine("<br/>");
            //sb.AppendFormat(Resources.Basket.UserBasketControl.DeliveryText, String.Format("{0:F}", orderHelperService.FreeDeliveryThreshold));

            return Content(sb.ToString());
        }      

    }
}
